﻿package com.projects.xenia;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.BaseAdapter;

import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.projects.xenia.add_activities.Activity_InfoTabs;
import com.projects.xenia.documents_activities.Activity_Blank;
import com.projects.xenia.documents_activities.Activity_Order;
import com.projects.xenia.documents_activities.Activity_Payment;
import com.projects.xenia.documents_activities.Activity_PhotoReport;
import com.projects.xenia.documents_classes.Documents_Visit;

import com.projects.xenia.global_modules.MainGlobalModule;
import com.projects.xenia.ref_classes.EmployeesRefObject;


public class MainRouteListActivity extends ListActivity {
	
	private EfficientAdapter adap;
	
	private int RouteDayOfRouteId;
	private String RouteDayOfRouteDescr;
	
	
	private String RouteManagerId;
	private String RouteManagerDescr;
	
	private Double RoutePosition;
	private Double RouteTimeShift;
	private Double RouteStartTime;
	
	private String CustomerId;
	private String CustomerDescr;
	private String CustomerOutletDescr;
	private String CustomerOutletAdress;
	private String CustomerOutletTelephone;
	private Double CustomerDeb;
	private Double CustomerDebOver;	
	
	private int stir_visit;
	private int stir_order;			
	private int stir_payment;
	private int stir_return;
	private int stir_photo;
	private int stir_task;
	private int stir_blank;
	
	private final int IDD_RouteActionsAlertDialog = 0;
	
	private int SelectedItemPosition;
	
	private ImageView doc_add;
	private ImageView r_plan;
	private ImageView r_first;
	
	
	
	
	String sDayOfRoute;
	String sManager;
	
	private Context Ctx;
	
	private HashMap <String , VisitItem> CustomerVisits;
	
	private class VisitItem {
		
		public String customer_id; 
		public int visit_state;		
	}	
	
	private void GetVisits () {
	
		CustomerVisits = new HashMap<String, VisitItem>();
		
		Cursor VisitsCursor = MainGlobalModule.DBE.rawQuery("select customers_id, visit_state from xdoc_visits_h where date (date_create) = date ('now') ", null); 
		if (VisitsCursor.getCount()>0) {
			VisitsCursor.moveToFirst();
			while (!VisitsCursor.isAfterLast()){				
				VisitItem vIt = new VisitItem ();
				vIt.customer_id = VisitsCursor.getString(0); 
				vIt.visit_state = VisitsCursor.getInt(1);
				CustomerVisits.put(vIt.customer_id, vIt);
				VisitsCursor.moveToNext();
			}
		}
		
	}
	
	public void GetCursorData (int position) {
		
		RouteCursor.moveToPosition(position);
		
		RoutePosition = RouteCursor.getDouble(0);
		RouteTimeShift = RouteCursor.getDouble(1);
		
		CustomerId = RouteCursor.getString(2);			
		CustomerDescr = RouteCursor.getString(3);
		CustomerOutletDescr = RouteCursor.getString(4);
		CustomerOutletAdress = RouteCursor.getString(5);			
		
		CustomerDeb = RouteCursor.getDouble(6);
		CustomerDebOver = RouteCursor.getDouble(7);
		
		stir_order = RouteCursor.getInt(8);		
		stir_payment = RouteCursor.getInt(9);
		stir_return = RouteCursor.getInt(10);
		stir_photo = RouteCursor.getInt(11);
		stir_task = RouteCursor.getInt(12);
		stir_blank = RouteCursor.getInt(13);
		
	}
	
	public void GetRouteCursor () {
				
		try {			
			RouteCursor = MainGlobalModule.DBE.rawQuery("SELECT ifnull (rout.position,0), ifnull (rout.timeshift,0), docs.cst, cust.descr, cust.outletdescr, cust.outletadress, ifnull (f.restsum,0), ifnull(d.restsumover,0), ifnull (docs.ord,0),ifnull (docs.pay,0),ifnull (docs.ret,0), ifnull (docs.pht,0), 0 as tsk , ifnull (docs.blnk,0) as blnk FROM (SELECT que.cst AS cst,sum (que.rou) AS rou,sum (que.ord) AS ord, sum (que.pay) AS pay, sum (que.ret) AS ret , sum (que.bln) as blnk , sum (que.pht) as pht FROM (SELECT customers_id as cst, SUM (1) AS rou, SUM (0) AS ord, SUM(0) AS pay, SUM(0) AS ret, SUM(0) AS bln , SUM(0) AS pht FROM xref_ROUTES WHERE position <> 0 and dayofroute = ? and employees_id = ? GROUP BY customers_id UNION SELECT customers_id AS cst, SUM (0) AS rou, COUNT(*) AS ord, SUM(0) AS pay, SUM(0) AS ret, SUM(0) AS bln,SUM(0) AS pht FROM xdoc_ORDERS_H WHERE date (date_create) = date('now') and ((state&2)=0) GROUP BY customers_id UNION SELECT customers_id AS cst, SUM (0) AS rou, SUM(0) AS ord, COUNT(*) AS pay, SUM(0) AS ret, SUM(0) AS bln,SUM(0) AS pht FROM xdoc_PAYMENTS_H WHERE date (date_create) = date('now') and ((state&2)=0) GROUP BY customers_id UNION SELECT customers_id AS cst, SUM (0) AS rou, SUM(0) AS ord, SUM(0) AS pay, COUNT(*) AS ret, SUM(0) AS bln,SUM(0) AS pht FROM xdoc_RETURNS_H	WHERE date (date_create) = date('now') and ((state&2)=0) GROUP BY customers_id UNION SELECT customers_id AS cst, SUM (0) AS rou, SUM(0) AS ord, SUM(0) AS pay, sum (0) AS ret, count(*) AS bln,SUM(0) AS pht FROM xdoc_BLANKS_H WHERE date (date_create) = date('now') and ((state&2)=0) GROUP BY customers_id UNION SELECT customers_id AS cst, SUM (0) AS rou, SUM(0) AS ord, SUM(0) AS pay, sum (0) AS ret, SUM(0) AS bln,count(*) AS pht FROM xdoc_PHOTOREPORTS_H WHERE date (date_create) = date('now') and ((state&2)=0) GROUP BY customers_id) que GROUP BY que.cst) docs INNER JOIN xref_CUSTOMERS cust on docs.cst = cust.id LEFT OUTER JOIN xref_ROUTES rout on (docs.cst = rout.customers_id and rout.position <> 0 and rout.dayofroute = ? and employees_id = ?) LEFT OUTER JOIN (SELECT b.customers_id as customers_id,sum( b.restsum) as restsumover FROM xref_debts b WHERE date (b.docpaydate) <= date('now') GROUP BY b.customers_id) d on docs.cst = d.customers_id LEFT OUTER JOIN (SELECT e.customers_id as customers_id,sum( e.restsum) as restsum FROM xref_debts e GROUP BY e.customers_id) f on docs.cst = f.customers_id ORDER BY rout.position,rout.timeshift,cust.descr", new String [] {sDayOfRoute,sManager,sDayOfRoute,sManager});			
			RouteCursor.moveToFirst();
			RouteStartTime = (double) 0;
			} catch (Exception e) {	
				e.printStackTrace();
		}		
		
	}
	
	private void GetEmployeesCursor () {
		
		try {
			EmployeesCursor = MainGlobalModule.DBE.rawQuery("select id as _id,descr from xref_employees order by def", null);
			EmployeesCursor.moveToFirst();
			} catch (Exception e) {				
		}
	}

	private void GetDayOfWeekCursor () {
		
		Map<String, Object> map;
		
		map = new HashMap<String, Object>();
		map.put("_id", "1");
		map.put("descr", "Понедельник");
		DaysOfWeekData.add(map);
		
		map = new HashMap<String, Object>();
		map.put("_id", "2");
		map.put("descr", "Вторник");
		DaysOfWeekData.add(map);

		map = new HashMap<String, Object>();
		map.put("_id", "3");
		map.put("descr", "Среда");
		DaysOfWeekData.add(map);

		map = new HashMap<String, Object>();
		map.put("_id", "4");
		map.put("descr", "Четверг");
		DaysOfWeekData.add(map);

		map = new HashMap<String, Object>();
		map.put("_id", "5");
		map.put("descr", "Пятница");
		DaysOfWeekData.add(map);

		map = new HashMap<String, Object>();
		map.put("_id", "6");
		map.put("descr", "Суббота");
		DaysOfWeekData.add(map);

		map = new HashMap<String, Object>();
		map.put("_id", "7");
		map.put("descr", "Воскресение");
		DaysOfWeekData.add(map);		
		
		
	}
	
	private List<Map<String, ?>> DaysOfWeekData = new ArrayList<Map<String, ?>>();

	private Cursor RouteCursor;
	
	private Cursor EmployeesCursor; 
		
	private Spinner _RouteDayOfWeekSpinner;
	
	private Spinner _RouteAgentSpinner;
		
	private SimpleAdapter _DayOfWeekAdapter;
	
	private SimpleCursorAdapter _RouteAgentAdapter;

	private ImageView _BackWardButton;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
        requestWindowFeature(Window.FEATURE_NO_TITLE);            
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.route_form);		
		
		Ctx = this;
				
		GetEmployeesCursor ();
		GetDayOfWeekCursor ();		
		
		_BackWardButton = (ImageView)findViewById(R.id.cust_backward);
		_BackWardButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		
		_RouteDayOfWeekSpinner = (Spinner)findViewById (R.id.RouteDayOfWeekSpinner);		
		_DayOfWeekAdapter = new SimpleAdapter (this,DaysOfWeekData,android.R.layout.simple_spinner_item, new String[] {"descr"},new int[] {android.R.id.text1});				
		_DayOfWeekAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		_RouteDayOfWeekSpinner.setAdapter(_DayOfWeekAdapter);		
		_RouteDayOfWeekSpinner.setSelection(Calendar.getInstance().get(Calendar.DAY_OF_WEEK)-2);
		_RouteDayOfWeekSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {			
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {				
				sDayOfRoute = (String)DaysOfWeekData.get(arg2).get("_id");
				GetRouteCursor();				
				adap.notifyDataSetInvalidated();
				adap.notifyDataSetChanged();
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {				
			}			
		});
		
		_RouteAgentSpinner = (Spinner)findViewById (R.id.RouteAgentSpinner);
		_RouteAgentAdapter = new SimpleCursorAdapter (this,android.R.layout.simple_spinner_item,EmployeesCursor,new String[] {"descr"},new int[] {android.R.id.text1});
		_RouteAgentAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		_RouteAgentSpinner.setAdapter(_RouteAgentAdapter);
		_RouteAgentSpinner.setSelection(0);		
		_RouteAgentSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
				EmployeesCursor.moveToPosition(arg2);
				sManager = EmployeesCursor.getString (0);
				GetRouteCursor();				
				adap.notifyDataSetInvalidated();
				adap.notifyDataSetChanged();				
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {				
			}
		});		
		
		sDayOfRoute = (String)DaysOfWeekData.get(_RouteDayOfWeekSpinner.getSelectedItemPosition()).get("_id");
		sManager = EmployeesCursor.getString (0);
		
		GetRouteCursor ();
		
		adap = new EfficientAdapter(this);
		setListAdapter(adap);		
		this.getListView().setOnItemClickListener(onItemClickListener);
		
		MainGlobalModule.setBackGround(getListView());
		
		r_first = (ImageView)findViewById(R.id.r_first);
		r_first.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				if (!MainGlobalModule.CheckLocationToday (true)) {
					Intent GPSLocationActivityIntent = new Intent ();    	
	        		GPSLocationActivityIntent.setClass(Ctx, GPSLocationActivity.class);    			    	
	        	    startActivityForResult(GPSLocationActivityIntent, 0);	
				}  				
			}
		});
		

		doc_add = (ImageView)findViewById(R.id.doc_add);
		doc_add.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				SelectedItemPosition = -1; 
				showDialog(IDD_RouteActionsAlertDialog);
			}
		});
		
		GetVisits ();		
		SetButtonsVisibility ();
		
	}
	
	private void SetButtonsVisibility () {
		
		if (MainGlobalModule.CheckLocationToday(true)) {
			r_first.setVisibility(View.GONE);
			doc_add.setVisibility(View.VISIBLE);
		}
		else {
			r_first.setVisibility(View.VISIBLE);
			doc_add.setVisibility(View.GONE);
		}
		
	}
	
	private OnItemLongClickListener onListItemLongClick = new OnItemLongClickListener() {
		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int position, long arg3) {
			
			SelectedItemPosition = position; 
			showDialog(IDD_RouteActionsAlertDialog);		
			return true;
			
		}
		
	};
	
	private OnItemClickListener onItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

			SelectedItemPosition = position; 
			GetCursorData (SelectedItemPosition);
			OpenClientInfo(CustomerId);
			
		}
		
	};
	
	protected Dialog onCreateDialog(int id) {
		
		AlertDialog.Builder adb = new AlertDialog.Builder(this);
		
		switch (id) {		
			
			case IDD_RouteActionsAlertDialog:			
			adb.setItems(new String [] {"Заказ","Оплата","Возврат","Фотоотчет","Задача","Анкетирование","Долги","Информация"} ,ActionSelectDialogListener);
			return adb.create();
		
			}
		
		return null;
		
	}
	
	private DialogInterface.OnClickListener ActionSelectDialogListener = new DialogInterface.OnClickListener() {
	    public void onClick(DialogInterface dialog, int which) {
	    	
	    	if (SelectedItemPosition>-1) {
	    		GetCursorData (SelectedItemPosition);	
	    	}
	    	else
	    	{
	    		CustomerId = null;
	    	}	    	
	    	
	    	switch (which) {
    		case 0:
    			CreateOrderDocument (CustomerId);
				break;
    		case 1:
    			CreatePaymentDocument (CustomerId);
				break;
    		case 2:
    			CreateReturnDocument (CustomerId);
				break;
    		case 3:
    			CreatePhotoDocument (CustomerId);
				break;
    		case 5:
    			CreateBlankDocument (CustomerId);
				break;
    		case 6:
    			OpenDebts(CustomerId);
				break;
    		case 7:
    			OpenClientInfo(CustomerId);
				
	    	}
	  }		
	};
	
	private void OpenClientInfo(String pCustomerId) {
		
		if (pCustomerId!=null) {
			Intent InfoTabs = new Intent ();
			InfoTabs.setClass(Ctx,Activity_InfoTabs.class);		
			InfoTabs.putExtra("customer_id", pCustomerId);
			InfoTabs.putExtra("dayofroute",Integer.parseInt(sDayOfRoute));
			InfoTabs.putExtra("journaltab", true);
			startActivityForResult(InfoTabs, 0);				    		
		}
		
	}
	
	private void CreateBlankDocument(String pCustomerId) {
		   
    	Intent DocumentActivity = new Intent ();    	
    	DocumentActivity.setClass(this, Activity_Blank.class);
    	DocumentActivity.putExtra("CustomerId", pCustomerId);    	
    	startActivityForResult(DocumentActivity, 0);
    	
	}
	
	private void CreatePhotoDocument (String pCustomerId) {
		   
    	Intent DocumentActivity = new Intent ();    	
    	DocumentActivity.setClass(this, Activity_PhotoReport.class);
    	DocumentActivity.putExtra("CustomerId", pCustomerId);    	
    	startActivityForResult(DocumentActivity, 0);
    	
	}    
        
    private void CreateOrderDocument (String pCustomerId) {
        
    	Intent DocumentActivity = new Intent ();    	
    	DocumentActivity.setClass(this, Activity_Order.class);
    	DocumentActivity.putExtra("CustomerId", pCustomerId);    	
    	startActivityForResult(DocumentActivity, 0);    	
    }
    
    private void CreateReturnDocument (String pCustomerId) {
    
    	Intent DocumentActivity = new Intent ();    	
    	DocumentActivity.setClass(this, MainReturnActivity.class);
    	DocumentActivity.putExtra("CustomerId", pCustomerId);    	
    	startActivityForResult(DocumentActivity, 0);
	
	}
    
    private void CreatePaymentDocument (String pCustomerId) {

        
    	Intent DocumentActivity = new Intent ();    	
    	DocumentActivity.setClass(this, Activity_Payment.class);
    	DocumentActivity.putExtra("CustomerId", pCustomerId);    	
    	startActivityForResult(DocumentActivity, 0);
    	
    }
  
    private void OpenDebts(String lCustomerId) {
    	
    	Intent DebtsDetails = new Intent();
    	
    	if (lCustomerId!=null) {
			DebtsDetails.setClass(this,MainDebtsDetailsActivity.class);
			DebtsDetails.putExtra("Customer", lCustomerId);			
			startActivityForResult(DebtsDetails, 0);    		
    	}
    	else {    		
    		DebtsDetails.setClass(this,MainDebtsListActivity.class);
    		startActivityForResult(DebtsDetails, 0);
    	}
    		
    	
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	
    	super.onActivityResult(requestCode, resultCode, data);    	
    	
    	if (resultCode == RESULT_OK) {
    		boolean VisitsOn = Boolean.parseBoolean(MainGlobalModule.ConstantsCollection.get("VisitsOn"));
    		if (VisitsOn) {
    			CreateVisits ();	
    		}
    	};
    	
    	GetVisits ();
    	
    	SetButtonsVisibility ();
    	
    	RouteCursor.requery();
		adap.notifyDataSetChanged();
		
    }    
    
    private void CreateVisits () {
    	
    	
    	Calendar CurrentDate = Calendar.getInstance();
    	
    	int dayoftroute = CurrentDate.get(Calendar.DAY_OF_WEEK)-1;
    	
    	Cursor RouteCursor = MainGlobalModule.DBE.rawQuery("select id , employees_id , position , dayofroute , timeshift ,customers_id  from xref_Routes where dayofroute = ? order by position ",new String [] {String.valueOf(dayoftroute)});   	
    	    	
    	if (RouteCursor.getCount()>0) {
    		RouteCursor.moveToFirst();
    		
    		int position = 0;
    		
    		MainGlobalModule.DBE.beginTransaction();
    		
    		MainGlobalModule.DBE.delete("xdoc_Visits_h", null, null);
    		
    		while (!RouteCursor.isAfterLast()) {
    			
    			position ++;
    			
    			ContentValues InsertUpdateValues = new ContentValues ();
    			InsertUpdateValues.put("id", UUID.randomUUID().toString().toUpperCase());
    			InsertUpdateValues.put("docnum", position);		
    			InsertUpdateValues.put("date_create", MainGlobalModule.DateToSqlString(CurrentDate.getTime()));
    			InsertUpdateValues.put("date_save", MainGlobalModule.DateToSqlString(CurrentDate.getTime()));				
    			InsertUpdateValues.put("date_planned", MainGlobalModule.DateToSqlString(CurrentDate.getTime()));
    			InsertUpdateValues.put("state", 0);
    			InsertUpdateValues.put("verstamp", 0);
    			InsertUpdateValues.put("user_id",RouteCursor.getString(1));
    			InsertUpdateValues.put("employees_id", RouteCursor.getString(1));
    			InsertUpdateValues.put("customers_id", RouteCursor.getString(5));
    			InsertUpdateValues.put("visit_state", 0);
    			
    			MainGlobalModule.DBE.insert("xdoc_Visits_h", null, InsertUpdateValues);
    			
    			CurrentDate.add(Calendar.MINUTE, (int)RouteCursor.getDouble(4));
    			
    			RouteCursor.moveToNext();
    			
    		}
    		
    		MainGlobalModule.DBE.setTransactionSuccessful();
    		MainGlobalModule.DBE.endTransaction();
    		
    	}    	
    }
    
	public class EfficientAdapter extends BaseAdapter  {
		
		private LayoutInflater mInflater;
		public Context context;
		 
		public EfficientAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
			this.context = context;
		}
		 
		public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		
		if (convertView == null) {			
			convertView = mInflater.inflate(R.layout.route_form_list, null);
		}
		
		GetCursorData (position);
		
		holder = new ViewHolder();
		
		holder.RouteList_Position = (TextView) convertView.findViewById(R.id.RouteList_Position);
		holder.RouteList_Time = (TextView) convertView.findViewById(R.id.RouteList_Time);
		holder.RouteList_Customer = (TextView) convertView.findViewById(R.id.RouteList_Customer);
		holder.RouteList_Details = (TextView) convertView.findViewById(R.id.RouteList_Details);			
		holder.RouteList_Deb = (TextView) convertView.findViewById(R.id.RouteListDeb);
		holder.RouteList_DebOver = (TextView) convertView.findViewById(R.id.RouteListDebOver);				
	
		holder.RouteList_stir_visit = (ImageView) convertView.findViewById(R.id.stir_visit);
		holder.RouteList_stir_order = (ImageView) convertView.findViewById(R.id.stir_order);			
		holder.RouteList_stir_payment = (ImageView) convertView.findViewById(R.id.stir_payment);
		holder.RouteList_stir_return = (ImageView) convertView.findViewById(R.id.stir_return);
		holder.RouteList_stir_photo = (ImageView) convertView.findViewById(R.id.stir_photo);
		holder.RouteList_stir_task = (ImageView) convertView.findViewById(R.id.stir_task);
		holder.RouteList_stir_blank = (ImageView) convertView.findViewById(R.id.stir_blank);
			
		holder.RouteList_Position.setTextColor(RoutePosition>0?Color.YELLOW:Color.RED);
		
		holder.RouteList_Position.setText(RoutePosition>0?String.valueOf(RoutePosition):"*");
		holder.RouteList_Time.setText(RoutePosition>0?String.valueOf(RouteTimeShift):"");
		holder.RouteList_Customer.setText(CustomerDescr);
		holder.RouteList_Details.setText(CustomerOutletDescr + CustomerOutletAdress);			
			
		if (CustomerDeb!=0){ 
			holder.RouteList_Deb.setText(String.format("%.2f/",CustomerDeb));			
			holder.RouteList_DebOver.setText(String.format("%.2f",CustomerDebOver));
		}
		else {
			holder.RouteList_Deb.setText("");			
			holder.RouteList_DebOver.setText("");
		}
			
		
		VisitItem vIt = CustomerVisits.get (CustomerId);
		holder.RouteList_stir_visit.setVisibility(vIt!=null?View.VISIBLE:View.INVISIBLE);
		int stir_visit = 0;
		
		if (vIt!=null) {
			stir_visit = vIt.visit_state;
			switch (vIt.visit_state) {
			case 0:
				holder.RouteList_stir_visit.setVisibility(View.INVISIBLE);
				break;
			case 1:
				holder.RouteList_stir_visit.setImageResource(R.drawable.ic_visit_yellow);
				break;
			case 2:
				holder.RouteList_stir_visit.setImageResource(R.drawable.ic_visit_green);
				break;
			case 3:
				holder.RouteList_stir_visit.setImageResource(R.drawable.ic_visit_red);
				break;				
			default:
				break;
			}
		}
		
		holder.RouteList_stir_order.setVisibility(stir_order>0 ? View.VISIBLE:View.INVISIBLE);
		holder.RouteList_stir_payment.setVisibility(stir_payment>0 ? View.VISIBLE:View.INVISIBLE);
		holder.RouteList_stir_return.setVisibility(stir_return>0 ? View.VISIBLE:View.INVISIBLE);
		holder.RouteList_stir_photo.setVisibility(stir_photo>0 ? View.VISIBLE:View.INVISIBLE);
		holder.RouteList_stir_task.setVisibility(stir_task>0 ? View.VISIBLE:View.INVISIBLE);
		holder.RouteList_stir_blank.setVisibility(stir_blank>0 ? View.VISIBLE:View.INVISIBLE);
		
		
		if ((stir_order+stir_payment+stir_return+stir_photo+stir_task+stir_blank+stir_visit) > 0) {
			convertView.setBackgroundColor(Color.rgb(52,77,89));
		}
		else
		{
			convertView.setBackgroundColor(Color.TRANSPARENT);
		}		
				
		return convertView;
		
		}
		 
		class ViewHolder {		
			
			TextView RouteList_Position;
			TextView RouteList_Time;
			TextView RouteList_Customer;
			TextView RouteList_Details;			
			TextView RouteList_Deb;
			TextView RouteList_DebOver;			
		
			ImageView RouteList_stir_visit;	
			ImageView RouteList_stir_order;			
			ImageView RouteList_stir_payment;
			ImageView RouteList_stir_return;
			ImageView RouteList_stir_photo;
			ImageView RouteList_stir_task;
			ImageView RouteList_stir_blank;
		
		}
		 
				 
		@Override
		public long getItemId(int position) {
			
			return position;
			
		}
		 
		@Override
		public int getCount() {

			return RouteCursor.getCount();
		}
		 
		@Override
		public Object getItem(int position) {
			
			GetCursorData (position);
			
			return position;
		
		}	
	}
	
}
